//
//  MapTraverseCondition.cpp
//  DiceWar
//
//  Created by xiaofeng on 22/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "MapTraverseCondition.h"
#include "MapGraph.h"

AvailableCellsTraverseCondition::AvailableCellsTraverseCondition(const MapGraph::HexMapGridMatrix* pMapGridMatrix)
: m_pMapGridMatrix(pMapGridMatrix) {}

AvailableCellsTraverseCondition::~AvailableCellsTraverseCondition() {}

bool AvailableCellsTraverseCondition::operator()(int coordinateX, int coordinateY)
{
    if (!m_pMapGridMatrix) return false;
    if (coordinateX < 0 || coordinateX >= m_pMapGridMatrix->getSizeX()) return false;
    if (coordinateY < 0 || coordinateY >= m_pMapGridMatrix->getSizeY()) return false;
    
    const HexMapGrid& grid = m_pMapGridMatrix->getValue(coordinateX, coordinateY);
    if (!grid.isPlayable()) return false;
    
    return true;
}

PlayerCellsTraverseCondition::PlayerCellsTraverseCondition(const MapGraph::HexMapGridMatrix* pMapGridMatrix, PlayerType player)
: m_pMapGridMatrix(pMapGridMatrix)
, m_playerType(player) {}

PlayerCellsTraverseCondition::~PlayerCellsTraverseCondition(){}

bool PlayerCellsTraverseCondition::operator()(int coordinateX, int coordinateY)
{
    if (!m_pMapGridMatrix) return false;
    if (coordinateX < 0 || coordinateX >= m_pMapGridMatrix->getSizeX()) return false;
    if (coordinateY < 0 || coordinateY >= m_pMapGridMatrix->getSizeY()) return false;
    
    const HexMapGrid& grid = m_pMapGridMatrix->getValue(coordinateX, coordinateY);
    if (!grid.isPlayable()) return false;
    if (grid.getPlayerType() != m_playerType) return false;
    return true;
}

